﻿using System;
using System.Collections.Generic;
using System.Text;

namespace ContactManager.Common
{
    class DateUtil
    {
        //当天与该周星期一相差的天数
        public static int getModdayNum(System.DayOfWeek dw)
        {
            int weeknow = Convert.ToInt32(dw);
            int moddayNum = (-1) * weeknow + 1;
            return moddayNum;
        }
        //当天与该周星期日相差的天数
        public static int getSundayNum(System.DayOfWeek dw)
        {
            int weeknow = Convert.ToInt32(dw);
            int sundayNum = 7 - weeknow;
            return sundayNum;
        }
        //该周星期一的日期
        public static string getWeekMonday(DateTime date, System.DayOfWeek dw, string timeFormateStr)
        {
            int weeknow = Convert.ToInt32(dw);
            int moddayNum = (-1) * weeknow + 1;
            string weekMonday = date.AddDays(moddayNum).Date.ToString(timeFormateStr);
            return weekMonday;
        }
        //该周星期日的日期
        public static string getWeekSunday(DateTime date, System.DayOfWeek dw, string timeFormateStr)
        {
            int weeknow = Convert.ToInt32(dw);
            int sundayNum = 7 - weeknow;
            string weekSunday = date.AddDays(sundayNum).Date.ToString(timeFormateStr);
            return weekSunday;
        }
        //本周是本年第几周
        public static int weekNum(System.DayOfWeek dw,DateTime dt)
        {
            int weeknow = Convert.ToInt32(dw);//今天星期几
            int daydiff = (-1) * (weeknow + 1);//今日与上周末的天数差
            int days = dt.AddDays(daydiff).DayOfYear;//上周末是本年第几天
            int weeks = days / 7;

            if (days % 7 != 0)
            {
                weeks++;
            }
            return (weeks + 2);
        }

        public static string toChinaWeek(DateTime dt)
        {
            return dt.ToString("dddd", new System.Globalization.CultureInfo("zh-cn"));
        }
    }
}
